import store from '@/store'
import axios from 'axios'

// create an axios instance
const request = axios.create({
  // baseURL: process.env.VUE_APP_BASE_API,
  baseURL: 'https://slwl-geteway.itheima.net/manager',
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})

// request interceptor
request.interceptors.request.use(
  config => {
    const token = store.getters.token
    if (token) {
      config.headers.authorization = token
    }
    return config
  },
  error => {
    console.log(error)
    return Promise.reject(error)
  }
)

// response interceptor
request.interceptors.response.use(
  response => {
    return response.data.data
  },
  error => {
    return Promise.reject(error)
  }
)

export default request
